/*
Content: Table G.8
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Journal: Journal of Labor Economics
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/

		
********************************************************************************
*** LOAD ORIGINAL DATASET
********************************************************************************	

clear 
append "$intermediate/40perc_full_noger_prepared.dta"


********************************************************************************
*** CREATE MONTHLY PANEL
********************************************************************************

global cutoff "01-31 02-28 03-31 04-30 05-31 06-30 07-31 08-31 09-30 10-31 11-30 12-31"	
global years 1997/2018

	
* CREATE CUTOFF DATES 
foreach y of numlist $years {
foreach c of global cutoff {
	
preserve

scalar cdate = date("`c'-`y'", "MDY")
display _n(2) as result %td_CCYY-NN-DD cdate
display _dup(157) "-"


	********************************************************************************
	* Flag episodes that include the cutoff date
	********************************************************************************
	gen cutoff_incl = 0
	replace cutoff_incl = 1 if begepi <= cdate & endepi >= cdate

	sum cutoff_incl


	********************************************************************************
	* Keep only episodes that include the cutoff date
	********************************************************************************
	keep if cutoff_incl == 1

	********************************************************************************
	* Clean up
	********************************************************************************
	drop cutoff_incl
	
	
	* CREATE INDICATOR FOR THE CUTOFF MONTH
	gen cdate = "`y'_`c'"
	gen y = substr(cdate, 1,4)
	gen m = substr(cdate,6,2)
	
	destring y m, replace
	
	gen startmy = ym(y,m)
	
	drop cdate

	
* SAVE TEMPORARY FILE TO BE THEN APPENDED ALL TOGETHER 
save "$data/cmonth_`y'_`c'.dta", replace
	
restore 
}
}


********************************************************************************
**# APPEND ALL MONTHLY DATA TOGETHER
********************************************************************************

global cutoff "01-31 02-28 03-31 04-30 05-31 06-30 07-31 08-31 09-30 10-31 11-30 12-31"	
global years 2000/2017

clear 

* CREATE CUTOFF DATES 
foreach y of numlist $years {
foreach c of global cutoff {
	
append using "$data/cmonth_`y'_`c'.dta", keep(entry_spell begepi endepi age frau exit_spell persnr nat_first frau edu_max ao_bula quelle ieb_ao_krs_num ieb_wo_krs_num azu mar) nolabel

cap gen y_cutoff = .
cap gen md_cutoff = "" 

replace y_cutoff = `y' if y_cutoff == .
replace md_cutoff = "`c'" if md_cutoff == ""

drop if nat_first == 0 
	
}
}

********************************************************************************
**# Compute of leavers in each month-year-origin with the pre-post design from the IEB
********************************************************************************

* Generate startmy of the endepi 
cap drop month
rename y_cutoff year 

gen month = substr(md_cutoff, 1,2)
destring month, replace
gen startmy = ym(year, month)

bys persnr: gen leave = 1 if _n == _N
replace leave = 0 if leave != 1

rename nat_first ieb_nat_id 


save "$data/month_crosssection_ieb_noger_40perc.dta", replace
	
********************************************************************************	
**# ATTACH TERROR EVENTS + RUN SAME PROCEDURES TO DEFINE RELEVANT AND ISOLATED EVENTS (SOEP DESIGN) 
********************************************************************************	
	
merge m:1 ieb_nat_id startmy  using "$globalterror/intermediate/terror_monthlydata_ieb_final.dta", gen(eventmerge_m) update 
drop if eventmerge_m==2


**** Define what is an event: Pre-post event ***

foreach x in 30 60 90 {
	foreach event in terror {	
	foreach var in	 m5y m4y m3y {		
	cap drop post`event'_`var'_p`x' 
	g post`event'_`var'_p`x' =0 if distance`event'_`var'_p`x' <=0
	replace post`event'_`var'_p`x' =1 if distance`event'_`var'_p`x'>=1 & distance`event'_`var'_p`x'!=.
	}
	}
	}


	foreach event in terror     {	
	foreach var in	 m5y m4y m3y  {

	label def post`event'_`var'_p90 0 "Pre-Terror" 1 "Post-Terror", replace
	label val post`event'_`var'_p90 post`event'_`var'_p90
	label var post`event'_`var'_p90 "Terror"
		
	}
	}


*** Create distance from event in weeks and days ***

* Since treatment is in a month I want to consider number of week/days since tha last week/day if the treated month (treatment g) and number of week/days before the first week/day if the treated month (control g)

* From the date of the treatment gen a variable that equals the last day of the month and the first day of the month

g lastday=31 if inlist(month,1,3,5,7,8,10,12)
replace lastday=30 if inlist(month,4,6,9,11)
replace lastday=28 if month==2

g firstday=1 

foreach x in	30 60 90 {
foreach event in terror  {	
foreach var in	 m5y m4y m3y {

g month`event'_`var'_p`x'=month(dofm(relevant`event'_`var'_p`x')) // this creates the month of the date of the event
g year`event'_`var'_p`x'=year(dofm(relevant`event'_`var'_p`x')) // this creates the year of the date of the event

g lastd`event'_`var'_p`x'=mdy(month`event'_`var'_p`x',lastday,year`event'_`var'_p`x') if relevant`event'_`var'_p`x'!=. // this creates the last day of the month of treatment from which I want to calculate the distance in days for the treated
replace lastd`event'_`var'_p`x'=mdy(month`event'_`var'_p`x',30,year`event'_`var'_p`x') if relevant`event'_`var'_p`x'!=. & lastd`event'_`var'_p`x'==.

g firstd`event'_`var'_p`x'=mdy(month`event'_`var'_p`x',firstday,year`event'_`var'_p`x') if relevant`event'_`var'_p`x'!=. // this creates the first day of the month of treatment from which I want to calculate the distance in days for the control

format  lastd`event'_`var'_p`x' %td
format  firstd`event'_`var'_p`x' %td
}
}
}



	sort ieb_nat_id startmy
* From the date of the treatment gen a variable that equals the first week
foreach x in	30 60 90 {
foreach event in terror  {	
foreach var in	 m5y m4y m3y {
g dis_`event'_`var'_p`x'_a=-(lastd`event'_`var'_p`x' - startdate) if distance`event'_`var'_p`x'>0 & distance`event'_`var'_p`x'!=.
replace dis_`event'_`var'_p`x'_a=0 if distance`event'_`var'_p`x'==0
g dis_`event'_`var'_p`x'_b=startdate- firstd`event'_`var'_p`x' if distance`event'_`var'_p`x'<0 & distance`event'_`var'_p`x'!=.
g dis_`event'_`var'_p`x'=dis_`event'_`var'_p`x'_a
replace dis_`event'_`var'_p`x'=dis_`event'_`var'_p`x'_b if dis_`event'_`var'_p`x'_b!=. & dis_`event'_`var'_p`x'==.
}
}
}



*****************************************************************************
** Keep only events were there are people in the control and treatment group
*****************************************************************************
* Do this with the share of interviews before and after - if share =1 means all interviews took place in the  pre-period or all in post period
		
foreach var in	m5y m4y m3y {
foreach x in	90 60 30 {
* Sum by country of origin, event date and pre-porst, sum number of interviews
cap drop prepostinterterror_`var'_p`x' 
cap drop totinterterror_`var'_p`x'  
cap drop ratiointerterror_`var'_p`x'
cap drop mratiointerterror_`var'_p`x'
qui bys ieb_nat_id  postterror_`var'_p`x' relevantterror_`var'_p`x' : gen prepostinterterror_`var'_p`x'  = _N   if distanceterror_`var'_p`x'!=. // for a given country and event, this sums total interviews in pre-period (e.g. among control group) and post peridod (e.g. among treatment group)
qui bys ieb_nat_id relevantterror_`var'_p`x' : gen totinterterror_`var'_p`x'  = _N  if distanceterror_`var'_p`x'!=. // // for a given country and event, this sums total interviews 
qui g ratiointerterror_`var'_p`x'= prepostinterterror_`var'_p`x'/totinterterror_`var'_p`x' if distanceterror_`var'_p`x'!=.
qui bys ieb_nat_id relevantterror_`var'_p`x' : egen mratiointerterror_`var'_p`x'=max(ratiointerterror_`var'_p`x') if ratiointerterror_`var'_p`x'!=.
cap drop prepostinterterror_`var'_p`x' totinterterror_`var'_p`x'
}
}

	 
********************************************************************************
*** SAMPLE
********************************************************************************

drop if syear>2018
drop if syear<2000
drop if ieb_nat_id==0 // drop Germans


cap drop ao_bula 
gen ao_bula = int(ieb_ao_krs_num/1000)
tab ao_bula
replace ao_bula = . if ao_bula == 0

cap drop wo_land 
gen wo_bula = int(ieb_wo_krs_num/1000)
tab wo_bula
replace wo_bula = . if wo_bula == 0

replace ao_bula = wo_bula if ao_bula ==.
			
		
*** Keep sample	with and without controls constant
foreach var in m3y {
foreach x in	90 60 30 {
egen clusterterror_`var'_p`x'=group(relevantterror_`var'_p`x')
}
}


keep if inrange(distanceterror_m3y_p90,-3,3)


********************************************************************************
**# GENERATE COLLAPSED DATA
********************************************************************************	
	
	foreach stat in sum mean {
	
	preserve
	
	collapse (`stat') leave (firstnm) postterror_m3y_p90 distanceterror_m3y_p90 mratiointerterror_m3y_p90 clusterterror_m3y_p90, by(ieb_nat_id year month ao_bula)
	
	save "$final/collapsed_ieb_largecells_`stat'_leave_terror.dta", replace
	
	restore 

	}


********************************************************************************
**# Table G.8
********************************************************************************

eststo clear
local i=1	


foreach stat in sum mean {
	
use "$final/collapsed_ieb_largecells_`stat'_leave_terror.dta", clear 
	
egen cluster = group(year month ieb_nat_id) 

foreach var in  m3y   {

global select_reg_M "!inlist(ieb_nat_id,.,1) & inrange(distanceterror_`var'_p90,-3,3) & !inlist(distanceterror_`var'_p90,0) & inrange(year,2001,2016) & mratiointerterror_m3y_p90 != 1"
global controls "i.ieb_nat_id#i.year i.year#i.month i.ao_bula" 	


reghdfe leave postterror_m3y_p90 if $select_reg_M, vce(cluster cluster) absorb($controls)
 eststo m`i'
 
sum	 leave if $select_reg_M & postterror_m3y_p90==0
estadd scalar ymean`i' = r(mean) 
 
local i=`i'+1

}

}

esttab m* using "$table/tableG8_final.tex", star(* 0.10 ** 0.05 *** 0.01) se replace b(3) ///
keep(postterror_m3y_p90) varlabel(postterror_m3y_p90 "Post-Terror") ///
mtitles("Ln number of leavers" "Share of leavers") ///
scalars("nobs Observations" "ymean Mean Outcome") sfmt(%9.0fc %9.3f) ///
addnote("Origin country FE x Year FE: Yes" "Year FE x Month FE: Yes" "State FE: Yes" "Mean indiv. characteristics: Yes") ///
nonotes fragment



